/**
 * 设置支付密码的逻辑
 */
var first = true;

function SettingThePaymentPassword() {
	//$(".weui-btn").attr("disabled", false);
	if($("#userName").val().trim() == '') {
		$.toast("请输入正确的用户昵称!", "text");
		return false;
	}
	if($("#payPassword").val().trim() == '') {
		$.toast("支付密码不能为空!", "text");
		return false;
	}
	if($("#repassword").val().trim() == '') {
		$.toast("确认支付密码不能为空!", "text");
		return false;
	}
	if($("#payPassword").val().trim() != $("#repassword").val().trim()) {
		$.toast("两次密码不一致，请检查!", "text");
		return false;
	}
	var SettingThePaymentPasswordReqbody = {
		"payPassword": $("#payPassword").val().trim(),
		"repassword": $("#repassword").val().trim(),
		"userName": $("#userName").val().trim()
	};
	if(first) {
		first = false;
		$.ajax({
			type: "POST",
			url: ip + "platform/user/serverSetTradePassword.m",
			xhrFields: {
				withCredentials: true
			},
			crossDomain: true,
			data: SettingThePaymentPasswordReqbody,
			success: function(data) {
				if(data.statusCode == 0) {
					//$(".weui-btn").attr("disabled", true);
					$.toptip("设置支付密码成功！", 'success');
					//setTimeout('window.location.href = "homePage.html"', 100);
					signOutLogin();
				} else if(data.statusCode == 1000) {
					$.toptip("登录失效！", 'error');
					setTimeout('window.location.href = "signIn.html"', 1000);
				} else {
					$.toptip(data.resultDesc, 'error');
					first = true;
				}
			}
		});
	}
}
/**
 * 退出登录
 */
function signOutLogin() {
	$.ajax({
		type: "POST",
		url: ip + "platform/user/serverLogout.m",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		success: function(data) {
			if(data.statusCode == 0) {
				localStorage.clear();
				sessionStorage.clear();
				// 清除所有的cookie
				var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
				if(keys) {
					for(var i = keys.length; i--;) {
						document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
					}
				}
				setTimeout('window.location.href = "signIn.html"', 100);
			} else {
				$.toptip(data.resultDesc, 'error');
			}
		}
	});
}
/**
 * 获取推荐人信息
 */
function getinviteUserNameByMobile() {
	if(/^1(3|4|5|6|7|8|9)\d{9}$/.test($("#inviteCode").val().trim())) {
		number = "+86|" + $("#inviteCode").val().trim();
	} else {
		number = $("#inviteCode").val().trim();
	}
	var getinviteUserNameByMobileReqbody = {
		"type": 0,
		"mobile": number
	};
	$.ajax({
		type: "POST",
		url: ip + "platform/user/getinviteUserNameByMobile.m",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: getinviteUserNameByMobileReqbody,
		success: function(data) {
			if(data.statusCode == 0) {
				$("#name").text(data.resultDesc);
			} else if(data.statusCode == 1000) {
				$.toptip("登录失效！", 'error');
				setTimeout('window.location.href = "signIn.html"', 1000);
			} else {
				$.toptip(data.resultDesc, 'error');
			}
		}
	});
}
document.addEventListener('plusready', function() {
	var webview = plus.webview.currentWebview();
	plus.key.addEventListener('backbutton', function() {
		webview.canBack(function(e) {
			if(window.confirm('是否退出APP？')) {
				webview.close();
				return true;
			} else {
				return false;
			}
		})
	});
});